package com.cskaoyan.mall.service;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.bean.Keyword;
import com.cskaoyan.mall.bean.bo.KeywordListBO;
import com.cskaoyan.mall.bean.vo.GeneralItemsBean;
import com.cskaoyan.mall.mapper.KeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: dyj
 * @Date: 2021/1/11 9:32
 * @Description
 */
@Service
public class KeywordServiceImpl implements KeywordService{

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public GeneralItemsBean SelectListByPageLimit(KeywordListBO keywordListBO) {
        // 分页处理
        PageHelper.startPage(keywordListBO.getPage(),keywordListBO.getLimit());

        // 模糊查询处理
        if(keywordListBO.getKeyword() != null){

            keywordListBO.setKeyword("%" + keywordListBO.getKeyword().trim() + "%");
        }

        if(keywordListBO.getUrl() != null){
            keywordListBO.setUrl("%" + keywordListBO.getUrl().trim() + "%");
        }

        // 查询制造商信息
        List<Keyword> keywordList =  keywordMapper.selectList(keywordListBO);

        // 获取总数目
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywordList);

        int total = (int) keywordPageInfo.getTotal();

        return GeneralItemsBean.data(keywordList,total);
    }

    @Override
    public void insertKeyword(Keyword keyword) {

        // 设置更新和插入时间
        String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        keyword.setUpdateTime(nowtime);
        keyword.setAddTime(nowtime);

        // 插入 使用useGeneratedKey选项
        keywordMapper.insertSelective(keyword);
    }

    @Override
    public void updateKeyWordSelective(Keyword keyword) {

        // 设置更新时间
        String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        keyword.setUpdateTime(nowtime);

        keywordMapper.updateByPrimaryKeySelective(keyword);
    }

    @Override
    public void deleteKeywordById(Integer id) {
        keywordMapper.deleteByPrimaryKey(id);
    }
}
